<?php
namespace app\dboard\controller;
use think\Controller;
use think\facade\Cookie;
use think\facade\Session;
use think\facade\Url;
use think\Db;
class Export extends Controller 
{
    public $AdminAccount=[];
    public function initialize(){
        if(!Session::has('AdminAccount.AdminID')){
            $cookievalue=Cookie::get('ZSyadminauths');
            if(strlen($cookievalue)>2){
                $destr=uc_authcode($cookievalue,'DECODE','ZSyadmin');
                list($AdminID, $AdminName,$AdminPhone,$sid) = explode("\t",$destr);
                if(intval($AdminID) > 0){
                    $this->AdminAccount['AdminID']=$AdminID;
                    $this->AdminAccount['AdminName']=$AdminName;
                    $this->AdminAccount['sid']=$sid;//默认0
                    Session::set('AdminAccount',$this->AdminAccount);
                }
            }
        }else{
            $this->AdminAccount=Session::get('AdminAccount');
        }

        $returnUrl=Url::build('dboard/mlogin/sign');
        if(!isset($this->AdminAccount['AdminID']) || intval($this->AdminAccount['AdminID'])<=0 || !isset($this->AdminAccount['sid'])){
            return $this->redirect($returnUrl, 5, '请登入');
        }
    }
    //public function exportexcel($sid)
    //{
    //    $sid= isset($sid)?intval($sid):0;
    public function exportexcel()
    {
        //$sid= isset($input['sid'])?intval($input['sid']):0;
        if (!$this->AdminAccount['sid']) {
           $this->redirect('/dboard/booking/lists.html', 5, '缺少必要参数');
           exit;
        }
        $sid= $this->AdminAccount['sid'];
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
           $this->redirect('/dboard/booking/lists.html', 5, '无此门店');
           exit;
        }
        $order = $this->_export_condition_order();
        $field = $this->_export_condition_field();
        $data =  Db::name('appointment')->where([['sid','=',$sid],['status','=',0]])->field($field)->order($order)->select();

        $header = $this->_export_condition_header();
        $filename = $this->_export_condition_filename();
        require config('vender_path')."PHPExcel/Classes/PHPExcel.php";
        $objectPHPExcel = new \PHPExcel();
        //Excel表格式,这里简略写了8列
        $letter = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        //表头数组
        $tableheader = $header;
        //填充表头信息
        for ($i = 0; $i < sizeof($tableheader); $i++) {
            $objectPHPExcel->getActiveSheet()->setCellValue("$letter[$i]1", "$tableheader[$i]");
        }
        // 填充表格信息
        for ($i = 2; $i < sizeof($data) + 2; $i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key => $value) {
                if ($key == 'id') {
                    $objectPHPExcel->getActiveSheet()->setCellValue("$letter[$j]$i", " $value");
                } elseif($key == 'baby_name') {
                    $objectPHPExcel->getActiveSheet()->setCellValue("$letter[$j]$i", $value);
                } elseif($key == 'age') {
                    $objectPHPExcel->getActiveSheet()->setCellValue("$letter[$j]$i", $value);
                } elseif($key == 'tel') {
                    $objectPHPExcel->getActiveSheet()->setCellValue("$letter[$j]$i", $value);
                } elseif($key == 'content') {
                    $objectPHPExcel->getActiveSheet()->setCellValue("$letter[$j]$i", $value);
                } elseif($key == 'createtime') {
                    $objectPHPExcel->getActiveSheet()->setCellValue("$letter[$j]$i", date('Y/m/d H:i',$value));
                }
                $j++;
            }
        }
        //创建Excel输入对象
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename="' . $filename . '.xls"');
        header("Content-Transfer-Encoding:binary");
        $objWriter= \PHPExcel_IOFactory::createWriter($objectPHPExcel,'Excel5');
        $objWriter->save('php://output');
    }


    public function _export_condition_header()
    {
        return array('序号', '宝贝姓名', '年龄', '电话号码', '备注', '预约时间');
    }

    public function _export_condition_filename()
    {
        return "预约表";
    }

    public function _export_condition_field()
    {
        return "id, baby_name, age, tel, content,createtime";
    }

    public function _export_condition_order()
    {
        return 'id DESC';
    }

}
